home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / adaed / nyudemos / concord.ada next >
Encoding:
Text File  |  1996-01-30  |  4.7 KB  |  204 lines

  1. with text_io; use text_io;
  2. with list_package;
  3. procedure concordance is
  4.  
  5.     package int_io is new integer_io(integer); use int_io;
  6.     package line_list is new list_package(natural); use line_list;
  7.  
  8.     type vstring is access string;
  9.     type word_node;
  10.     type word_link is access word_node;
  11.     subtype alpha is character range 'A'..'z' ;
  12.     
  13.     type word_node is 
  14.                     -- For words in text as stored 
  15.       record                -- in binary search tree.
  16.     word: vstring;
  17.     left: word_link;        -- Link to left child.
  18.     right: word_link;        -- Link to right child.
  19.     times: integer;            -- Count of occurrences.
  20.     lines: list;            -- Header for line number list.
  21.       end record;
  22.  
  23.  
  24.     root: word_link := null;        -- Root of binary search tree.
  25.  
  26.     procedure tree_search(word: in vstring) is separate;
  27. -- Procedure to search for word in tree. If search fails, new node is
  28. -- created for it.
  29.  
  30.  
  31.     procedure print_node(node: in word_node) is separate;        
  32. -- Print out information pertaining to word in text.
  33.  
  34.  
  35.     procedure tree_traverse(node: in word_link) is separate;
  36. -- Procedure performs inorder traversal of binary tree.
  37.  
  38. function get_string return string is
  39.    n : character ;
  40.    buffer : string(1..100) ;
  41.    len: integer := 0 ;
  42.    
  43. begin
  44.    get(n) ;
  45.  
  46.    while (n not in alpha) loop  get(n) ; end loop ;
  47.  
  48.    while (n in alpha) loop
  49.        len := len + 1 ;
  50.        buffer(len) := n ;
  51.        if end_of_line then exit; end if ;
  52.        get(n) ;
  53.    end loop ;
  54.  
  55.    return buffer(1..len) ;
  56. end get_string ;
  57.                 
  58. begin
  59.     -- Read words from text file into binary search tree.
  60.     loop
  61.     declare
  62.         x: vstring := new string'(get_string);
  63.     begin
  64.         new_line;
  65.         put_line("Next word in text: ");
  66.         put_line(x.all);
  67.         tree_search(x);    -- Search for word in tree.        
  68.     end;
  69.     end loop;
  70.  
  71. exception 
  72.     when end_error => 
  73.     new_line(5);
  74.     put_line("Alphabetized list of words in text: ");
  75.     new_line(2);
  76.     tree_traverse(root);    -- Print out contents of tree.
  77.  
  78. end concordance;
  79.  
  80.  
  81. separate(concordance)                            
  82. procedure tree_search(word: in vstring) is
  83.  
  84.     linenum : natural;
  85.     cur_node: word_link;
  86.  
  87.     function make_node return word_link is
  88.     -- Enter new node in binary tree.
  89.     x : word_link;
  90.     q : list := empty_list;
  91.  
  92.     begin
  93.     append(q, natural(line(standard_input)));
  94.     x := new word_node'
  95.             ( word =>  word,
  96.               left =>  null, 
  97.               right => null, 
  98.               times => 1, 
  99.               lines => q);
  100.     return x;
  101.  
  102.     end make_node;
  103.  
  104.  
  105. begin
  106.     -- check if tree empty
  107.     if root = null then
  108.     put_line("make root node");
  109.     root := make_node;
  110.     return;
  111.     end if;
  112.  
  113.     cur_node := root;                -- Search nonempty tree.
  114.     loop 
  115.     put_line("node examined: ");
  116.     put_line(cur_node.word.all);
  117.     if cur_node.word.all = word.all then    -- Word already seen.
  118.         put_line("word already seen");
  119.         cur_node.times :=
  120.         cur_node.times + 1;
  121.         linenum := natural(line(standard_input));
  122.         if last(cur_node.lines) /= linenum then
  123.         -- Add line number to list if not already present.
  124.         append(cur_node.lines, linenum);
  125.         end if;
  126.         return;
  127.     elsif cur_node.word.all > word.all then
  128.         if cur_node.left = null then 
  129.         put_line("attach left child");
  130.         cur_node.left := make_node;     -- Attach left child.
  131.         return;
  132.         else                    -- Search left subtree.
  133.         put_line("search left subtree");
  134.         cur_node := cur_node.left;
  135.         end if;
  136.     else
  137.         if cur_node.right = null then      -- Attach right child.
  138.         put_line("attach right child");
  139.         cur_node.right := make_node;
  140.         return;
  141.         else                    -- Search right subtree.
  142.         put_line("search right subtree");
  143.         cur_node := cur_node.right;
  144.         end if;
  145.     end if;
  146.     end loop;
  147.  
  148. end tree_search;
  149.  
  150.  
  151.  
  152. separate(concordance)
  153. procedure print_node(node: in word_node) is
  154.  
  155.     procedure print_list is
  156.     -- Print out contents of (non-empty) line number list,
  157.     -- from front to rear.
  158.     
  159.     cur_lines : list;
  160.     item : natural;
  161.  
  162.     begin
  163.     cur_lines := node.lines;
  164.     put_line("Appears on line numbers: ");
  165.     loop
  166.         remove(cur_lines, item);
  167.         put(item);
  168.         put(" ");
  169.         if is_empty(cur_lines) then
  170.             new_line;
  171.         return;
  172.         end if;
  173.     end loop;
  174.  
  175.     end print_list;
  176.  
  177.  
  178. begin
  179.     put_line(node.word.all);
  180.     put_line("Number of times word appears: ");
  181.     put(node.times);
  182.     new_line;
  183.     print_list;            -- Print contents of line number list.
  184.     new_line;
  185.     return;
  186.  
  187. end print_node;
  188.  
  189.  
  190. separate(concordance)
  191. procedure tree_traverse(node: in word_link) is
  192. -- Inorder traversal of binary tree.
  193.  
  194. begin
  195.     if node = null then return; end if;
  196.  
  197.     tree_traverse(node.left);        -- Traverse left subtree.
  198.     print_node(node.all);
  199.     tree_traverse(node.right);        -- Traverse right subtree.
  200.  
  201.     return;
  202.  
  203. end tree_traverse;
  204.